﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Common;
using log4net;
using SqlSugar;
using AutoFacUtils;
using CashFlow.Admin.Def;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using CashFlow.Db.Repository;
using CashFlow.Admin.Service;

namespace CashFlow.Admin.Service.Implement
{
    public class AppointmentService : Service<AppointmentEntity>, IAppointmentService
    {
        private IAdminService GetAdminService() { return AutoFacHelper.Get<IAdminService>(); }
        private IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }

        public IList<UserInfoAndReferenceUserInfo> QueryTeamNotAppointmentUsers(int userId, string date)
        {
            var service = GetUserService();
            var ls = service.QueryAllChildUsersWithSameShopByUserId(userId);
            var userIds = ls.Select(x => x.UserInfo.Id).ToList();
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls2 = db.Queryable<AppointmentEntity>().
                    Where(x => x.Time.ToString().Substring(0, 10) == date && userIds.Contains(x.UserId)).ToList();
                var ls3 = ls.Where(x => !ls2.Exists(y => y.UserId == x.UserInfo.Id)).ToList();

                return ls3;
            }
        }

        public IList<UserInfoAndReferenceUserInfo> QueryTeamNotAppointmentUsers(string date)
        {
            var service = GetAdminService();
            var ui = service.GetLoginUserInfo();
            return QueryTeamNotAppointmentUsers(ui.ShopManagerUserId, date);
        }

        public IList<UserInfoAndReferenceUserInfo> QueryTeamAppointmentUsers(int userId)
        {
            string date = DateTime.Now.ToString("yyyy-MM-dd");
            var service = GetUserService();
            var ls = service.QueryAllChildUsersWithSameShopByUserId(userId);
            var userIds = ls.Select(x => x.UserInfo.Id).ToList();
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls2 = db.Queryable<AppointmentEntity>().
                    Where(x => x.Time.ToString().Substring(0, 10) == date && userIds.Contains(x.UserId)).ToList();
                var ls3 = ls.Where(x => ls2.Exists(y => y.UserId == x.UserInfo.Id)).ToList();

                return ls3;
            }
        }

        public IList<UserInfoAndReferenceUserInfo> QueryTeamAppointmentUsers()
        {
            var service = GetAdminService();
            var ui = service.GetLoginUserInfo();
            return QueryTeamAppointmentUsers(ui.ShopManagerUserId);
        }

        public IList<AppointmentUserInfoView2> QueryByWhere(int userId, string date)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<AppointmentEntity>()
                    .InnerJoin<UserInfoEntity>((a, u1) => a.UserId == u1.Id)
                    .InnerJoin<UserInfoEntity>((a, u1, u2) => u1.ReferenceInvitationCode == u2.InvitationCode)
                    .Where((a, u1, u2) => a.UserId == userId
                            && a.Time.ToString().Substring(0, 10) == date)
                    .Select((a, u1, u2) => new AppointmentUserInfoView2
                    {
                        UserInfo = u1,
                        AppointmentInfo = a
                    })
                    .ToList();
                return ls;
            }
        }

        public IList<AppointmentUserInfoView2> QueryByWhere(string date)
        {
            var service = GetAdminService();
            var ui = service.GetLoginUserInfo();
            return QueryByWhere(ui.ShopManagerUserId, date);
        }
    }
}
